﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestJ.Tool;

namespace TestJ.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;
        private readonly IWebHostEnvironment _env;
        private readonly IMemoryCache _cache;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IWebHostEnvironment env, IMemoryCache cache)
        {
            _logger = logger;
            _env = env;
            _cache = cache;
        }
        /// <summary>
        /// 图形验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string ValidateCode(int num)
        {
            var ms = VerificationCodeService.Create(out string code, num);
            var result = Convert.ToBase64String(ms.ToArray());
            return code + result;
        }
        [HttpGet]
        public object GetValidateCode(int num)
        {
            var key = Guid.NewGuid().ToString("N");
            var ms = VerificationCodeService.Create(out string code, num);
            var base64Png = Convert.ToBase64String(ms.ToArray());

            _cache.Set(key, code);
            return new
            {
                Key = key,
                Value = base64Png
            };
        }

        [HttpGet]
        public bool GetGoValidateCode(string key,string value)
        {
            var cacheValue = _cache.Get(key).ToString();
            _cache.Remove(key);
            return cacheValue.ToLower() == value.ToLower();
        }


        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var b = new TestAA { Bstr = "xxx" };
            var b22 = new TestAA { Bstr = "" };
            var b33 = new TestAA { Bstr = null };
            var c = new TestAA { StrAA = null };
            var d = new TestAA {  };

            var aaa = new TestAA { Cstr = "xxxx" };
            var aaa2 = new TestAA { Cstr = "" };
            var aaa3 = new TestAA { Cstr = null };


            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpGet]
        public string Say()
        {
            return $"hello----{DateTime.Now}--{_env.EnvironmentName}";
        }
    }
}
